package com.homer.iot.esptouch.model;

import java.util.List;

import com.homer.android.sdk.adt.tree.IEspDeviceTreeElement;
import com.homer.android.sdk.devcie.IEspDevice;
import com.homer.android.sdk.devcie.IEspDeviceConfigure;
import com.homer.android.sdk.devcie.IEspDeviceSSS;
import com.homer.android.sdk.devcie.bulider.BEspDeviceConfigure;
import com.homer.android.sdk.type.devcie.IEspDeviceStatus;
import com.homer.android.sdk.type.devcie.state.EspDeviceState;
import com.homer.android.sdk.type.devcie.status.EspStatusPlug;
import com.homer.android.sdk.type.user.builder.BEspUser;
import com.homer.iot.esptouch.udp.EspDeviceType;
import com.homer.iot.esptouch.udp.IOTAddress;



public class EspDeviceSSS extends EspDevice implements IEspDeviceSSS
{
    private IOTAddress mIOTAddress;
    
    private IEspDeviceStatus mStatus;
    
    // -1, -2, ... is used to activate softap device by direct connect,
    // 1, 2, ... is used by server
    private static long mIdCreator = -Long.MAX_VALUE / 2;
    
    private synchronized long getNextId()
    {
        return --mIdCreator;
    }
    
    public EspDeviceSSS(IOTAddress iotAddress)
    {
        init(iotAddress);
    }
    
    private void init(IOTAddress iotAddress)
    {
        mIOTAddress = iotAddress;
        
        EspDeviceState stateLocal = new EspDeviceState();
        stateLocal.addStateLocal();
        setDeviceState(stateLocal);
        setName(mIOTAddress.getSSID());
        setBssid(mIOTAddress.getBSSID());
        setInetAddress(mIOTAddress.getInetAddress());
        setDeviceType(mIOTAddress.getDeviceTypeEnum());
        setDeviceNumber(mIOTAddress.getBSSID());
        setId(getNextId());
        
        EspDeviceType deviceType = mIOTAddress.getDeviceTypeEnum();
        if (deviceType != null)
        {
            switch (deviceType)
            {
//                case LIGHT:
//                    mStatus = new EspStatusLight();
//                    break;
                case PLUG:
                    mStatus = new EspStatusPlug();
                    break;
                case UART:
                    mStatus = new EspStatusPlug();
                    break;
//                case REMOTE:
//                    mStatus = new EspStatusRemote();
//                    break;
//                case PLUGS:
//                    mStatus = new EspStatusPlugs();
//                    break;
//                case ROOT:
//                    break;
//                case FLAMMABLE:
//                    break;
//                case HUMITURE:
//                    break;
//                case VOLTAGE:
//                    break;
//                case NEW:
//                    break;
            }
        }
    }
    
    @Override
    public void setIOTAddress(IOTAddress iotAddress)
    {
        mIOTAddress = iotAddress;
        
        init(iotAddress);
    }
    
    @Override
    public IOTAddress getIOTAddress()
    {
        return mIOTAddress;
    }
    
    @Override
    public IEspDeviceStatus getDeviceStatus()
    {
        return mStatus;
    }
    
    @Override
    public List<IEspDeviceTreeElement> getDeviceTreeElementList()
    {
        List<IEspDevice> allDeviceList = BEspUser.getBuilder().getInstance().getAllDeviceList();
        return getDeviceTreeElementList(allDeviceList);
    }
    
    @Override
    public IEspDeviceConfigure createConfiguringDevice(String random40)
    {
        IEspDeviceConfigure device = BEspDeviceConfigure.getInstance().alloc(this.mBssid, random40);
        device.setInetAddress(mInetAddress);
//        device.setIsMeshDevice(mIsMeshDevice);
//        device.setParentDeviceBssid(mParentDeviceBssid);
        device.setName(mDeviceName);
        return device;
    }
    
}

